package com.tinymce.imageupload;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import java.util.*;

@Service
public class FileUploadServiceImp implements FileUploadService {

    private static final Logger logger = LoggerFactory.getLogger(FileUploadService.class);
    private static final String IMAGE_URL_PREFIX = "/upload/";
    @Override
    public Map<String, String> upload(MultipartFile file, String path, String fileType) {
        String json="";
        String filename = file.getOriginalFilename();
        // 获取文件扩展名，如 abc.de.jpg，就获取 jpg
        String extensionName = filename.substring(filename.lastIndexOf(".") + 1);
        // 设置上传文件的文件名，防止命名冲突导致覆盖
        String uploadFilename = UUID.randomUUID().toString() + "." + fileType;

        logger.info("开始上传文件，\t文件原名：{}，上传路径：{}，新文件名：{}",
                filename, path, uploadFilename);

        File uploadDir = new File(path);
        // 检查上传路径是否存在，不存在则创建
        if (!uploadDir.exists()) {
            logger.info("上传路径不存在，创建路径");
            // 设置可读权限，因为启用 tomcat 的用户可能没有写文件的权限
            uploadDir.setWritable(true);
            boolean createDirResult = uploadDir.mkdirs();
            logger.info("文件的读权限：{}，文件的写权限：{}，创建结果：{}",
                    uploadDir.canRead(), uploadDir.canWrite(), createDirResult);
        }

        File targetFile = new File(uploadDir, uploadFilename);
        Map<String,String> map=new HashMap<>();
        try {
            file.transferTo(targetFile);
            // TODO 将 targetFile 上传到文件服务器上，上传完成后，删除 uploadDir 下的文件
            // targetFile.delete();

            map.put("stat", "1");
            map.put("msg", "文件上传成功");
            map.put("location", IMAGE_URL_PREFIX +targetFile.getName());
            map.put("filename",filename);
            /**
             * 获取图片的宽高
             */
            String[] strArr = {"png", "jpg", "bmp","gif"};
            boolean res = Arrays.asList(strArr).contains(fileType); // true
            if(res){
                //获取图片大小
                BufferedImage sourceImg = ImageIO.read( new FileInputStream( targetFile ) );
                double width = sourceImg.getWidth();
                double height = sourceImg.getHeight();
                double size = file.getSize();
                map.put("width", width+"");
                map.put("height", height+"");
                map.put("size", size+"");
                //json="{\"stat\":1,\"msg\":\"文件上传成功\",\"location\":\""+IMAGE_URL_PREFIX +targetFile.getName()+"\",\"width\":\""+width+"\",\"height\":\""+height+"\"}";
            }
            logger.info("文件路径：{}，文件名：{}", targetFile.getAbsolutePath(), targetFile.getName());
            return map;
        } catch (IOException e) {
            map.put("stat","0");
            map.put("msg","文件上传异常");
            map.put("location","");
            logger.error("文件上传异常：", e);
            return map;

        }

    }

}
